/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.academik.validation;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.academik.controller.ConsultaMatriculaBean;
import org.academik.controller.MatriculaBean;
import org.academik.util.FacesUtil;

/**
 *
 * @author Javier
 */
@FacesValidator("pagosValidator")
public class PagosValidator implements Validator {

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        MatriculaBean mb;
        ConsultaMatriculaBean cmb;

        String bean = (String) UIComponent.getCurrentComponent(FacesContext.getCurrentInstance()).getAttributes().get("bean");
        if ("matriculaBean".equals(bean)) {
            mb = (MatriculaBean) FacesUtil.getBean("matriculaBean");
            if (mb.getArregloDetalles().isEmpty()) {
                FacesMessage msg = new FacesMessage("Error:", "Registre el cobro de la matricula");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(msg);
            }
        } else {
            cmb = (ConsultaMatriculaBean) FacesUtil.getBean("consultaMatriculaBean");
            if (cmb.getArregloDetalles().isEmpty()) {
                FacesMessage msg = new FacesMessage("Error:", "Registre el cobro de la matricula");
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                throw new ValidatorException(msg);
            }
        }
    }
}
